package com.lfk.justweengine.engine;


import com.lfk.justweengine.utils.io.GameIO;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Rect;

import java.io.IOException;
import java.io.InputStream;

/**
 * 封装图片的获取
 * Created by liufengkai on 15/11/27.
 */
public class GameTexture {
    private Context e_context;
    private PixelMap e_bitmap;
    private GameIO gameIO;

    public GameTexture(Context e_context) {
        this.e_context = e_context;
        this.e_bitmap = null;
        this.gameIO = new GameIO(e_context);
    }

    public PixelMap getBitmap() {
        return e_bitmap;
    }

    public GameTexture(PixelMap e_bitmap) {
        this.e_bitmap = e_bitmap;
    }

    /**
     * get bitmap form assets
     *
     * @param filename
     * @return
     */
    public boolean loadFromAsset(String filename) {
        RawFileEntry rawFileEntry = e_context.getResourceManager().getRawFileEntry(filename);
        try {
            Resource rs = rawFileEntry.openRawFile();
            int length = rs.available();
            byte[] buffer = new byte[length];
            rs.read(buffer,0,length);
            ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
            srcOpts.formatHint = "image/png";

            ImageSource imageSourceNoOptions = ImageSource.create(buffer, srcOpts);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            e_bitmap = imageSourceNoOptions.createPixelmap(decodingOptions);
            imageSourceNoOptions.release();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    /**
     * get bitmap from a big bitmap
     *
     * @param filename
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public boolean loadFromAssetStripFrame(String filename,
                                           int x, int y,
                                           int width, int height) {
        InputStream inputStream;
        try {
            inputStream = gameIO.readAsset(filename);
            ImageSource.SourceOptions opts = new ImageSource.SourceOptions();
            opts.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(inputStream,opts);
            inputStream.close();
            ImageSource.DecodingOptions opts1 = new ImageSource.DecodingOptions();
            opts1.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap sourcePixelmap = imageSource.createPixelmap(opts1);
            Rect rect = new Rect(x, y, width, height);
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.pixelFormat = PixelFormat.ARGB_8888;
            e_bitmap = PixelMap.create(sourcePixelmap,rect,options);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static PixelMap loadFromBigBitmap(GameTexture texture, int x, int y
            , int w, int h) {
        Rect rect = new Rect(x, y, w, h);
        PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
        options.pixelFormat = PixelFormat.ARGB_8888;
        return PixelMap.create(texture.getBitmap(), rect, options);
    }

    public void setBitmap(PixelMap e_bitmap) {
        this.e_bitmap = e_bitmap;
    }

    /**
     * 从大图取出小图
     *
     * @param x 横轴
     * @param y 纵轴
     * @param w 宽
     * @param h 高
     * @return
     */
    public GameTexture getFrameFromBitmap(int x, int y,
                                          int w, int h) {
        if (e_bitmap != null) {
            PixelMap.InitializationOptions options = new PixelMap.InitializationOptions();
            options.pixelFormat = PixelFormat.ARGB_8888;
            Rect rect = new Rect(x, y, w, h);
            PixelMap bitmap =
                    PixelMap.create(e_bitmap, rect, options);
            return new GameTexture(bitmap);
        }
        return null;
    }
}
